package com.example.smartcitymovie.ui.movie;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.bean.MovieMessageBean;
import com.example.smartcitymovie.dataRepo.ApiData;
import com.example.smartcitymovie.databinding.ActivityMovieMessageBinding;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MovieMessageActivity extends AppCompatActivity {

    ActivityMovieMessageBinding binding;
    private int id;
    private boolean isExpand = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       binding = ActivityMovieMessageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getSupportActionBar().setTitle("影片详情");
        id = getIntent().getIntExtra("id", 0);
        initView();
    }

    private void initView() {
        loadMessage();
        binding.btnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MovieMessageActivity.this,ChooseMovieThreaterActivity.class);
                Log.d("TAG", "onClick: "+id);
                intent.putExtra("id",id);
                startActivity(intent);

            }
        });
    }

    private void loadMessage() {
        MovieMessageBean movieMessageBean = ApiData.getMovieDetailDataById(id);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide.with(binding.IVMovieCover).load(movieMessageBean.getData().getCover()).placeholder(R.drawable.ic_baseline_broken_image_24).into(binding.IVMovieCover);
                binding.RBScore.setRating(movieMessageBean.getData().getScore());
                binding.TVFavoriteNum.setText(String.valueOf(movieMessageBean.getData().getFavoriteNum())+"人");
                binding.TVLikeNum.setText(String.valueOf(movieMessageBean.getData().getLikeNum())+"人");
                binding.TVMovieName.setText(movieMessageBean.getData().getName());
                binding.TVPlayDate.setText(movieMessageBean.getData().getPlayDate());
                MyApplication.getMovieType().forEach((key, value) -> {
                    if (Integer.valueOf(movieMessageBean.getData().getPlayType())==key){
                        binding.TVMovieType.setText(value);
                    }
                });
                binding.ExText.setText(movieMessageBean.getData().getIntroduction());
            }
        });

        binding.btnExpand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isExpand){
                    isExpand = false;
                    binding.ExText.setMaxLines(2);
                    binding.btnExpand.setImageResource(R.drawable.ic_baseline_arrow_drop_down_24);
                }else{
                    binding.ExText.setMaxLines(10);
                    isExpand = true;
                    binding.btnExpand.setImageResource(R.drawable.ic_baseline_arrow_drop_up_24);
                }
            }
        });
    }
}